[php] Firefox refreshes page 1, even after redirection to page 2

Posted by Znarkus on Stack Overflow See other posts from Stack Overflow or by Znarkus
Published on 2010-05-11T18:59:56Z Indexed on 2010/05/11 19:04 UTC
Read the original article Hit count: 230

Filed under:
|
|

Hi! This is a quite weird and annoying problem, which is reproduced with the script below.

  1. Say we have two pages: script.php and script.php?second.
  2. Page 1 creates some database entries and redirects to page 2.
  3. On page 2, the user is presented with an editor for said entries.

If page 1 for some reason crashes on the first try, and prints some error message, a strange thing will happend. If we refresh page 1 (and this time it redirects fine), every consecutive refresh (of page 2) will actually refresh page 1 and again redirect to page 2.

In the above example this would create new database entries for every refresh, which is the problem I want to circumvent by redirecting to page 2.

<?php

header('Content-type: text/plain');

session_start();

if (!isset($_GET['second'])) {

    $_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter'] + 1 : 1;
    /*$_SESSION['counter'] = 0;
    exit('asd');*/
    header("Location: {$_SERVER['PHP_SELF']}?second", true, 303);
    exit;

}

echo "Counter: {$_SESSION['counter']}";

To try the above complete script, first run it with the commented code intact, then by enabling the commented code.

I've tried 301, 302 and 303 redirections. Does someone know why this is happening?

© Stack Overflow or respective owner

Related posts about php

Related posts about redirect